package com.lastproject.ejb.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.CandidateDTO;
import com.lastproject.dto.CvDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.entities.Candidate;
import com.lastproject.ejb.entities.User;
import com.lastproject.ejb.util.CandidateConversion;

/**
 * Session Bean implementation class CandidateDAO Author: Hanne
 */
@Stateless
public class CandidateDAO implements CandidateDAORemote {

	@PersistenceContext
	EntityManager em;

	@EJB
	CandidateConversion conv;

	/**
	 * Default constructor.
	 */
	public CandidateDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void create(CandidateDTO candidateDTO) {
		// TODO Auto-generated method stub
		em.persist(conv.fromDTO(candidateDTO));
	}

	@Override
	public void edit(CandidateDTO candidateDTO) {
		// TODO Auto-generated method stub
		em.merge(conv.fromDTO(candidateDTO));
	}

	@Override
	public void remove(CandidateDTO candidateDTO) {
		// TODO Auto-generated method stub
		em.remove(em.merge(conv.fromDTO(candidateDTO)));
	}

	@Override
	public CandidateDTO findById(int id) {
		// TODO Auto-generated method stub
		Candidate candidate = new Candidate();

		candidate = em.find(Candidate.class, id);

		return conv.fromEntity(candidate);
	}

	@Override
	public List<CandidateDTO> getAll() {
		// TODO Auto-generated method stub
		List<Candidate> candidateList = new ArrayList<Candidate>();

		candidateList = em.createNamedQuery("Candidate.findAll",
				Candidate.class).getResultList();

		List<CandidateDTO> cDTOList = new ArrayList<CandidateDTO>();

		for (Candidate c : candidateList) {
			cDTOList.add(conv.fromEntity(c));
		}

		return cDTOList;
	}

	@Override
	public byte[] findCv(int id) {

		List<CvDTO> l = em

				.createQuery(

						"Select new com.lastproject.dto.CvDTO(c.cv) From Candidate c where c.id =:id",

						CvDTO.class).setParameter("id", id).getResultList();

		System.out.println("FILE ce trebuie deschis " + l.get(0).getCv());

		return l.get(0).getCv();

	}

	@Override
	public CandidateDTO findByFullName(String firstname, String lastname) {
		List<Candidate> list = em
				.createQuery(
						"Select c from Candidate c "
								+ "where c.firstname = :firstname "
								+ "and c.lastname = :lastname", Candidate.class)
				.setParameter("firstname", firstname)
				.setParameter("lastname", lastname).getResultList();

		if (list.size() > 0) {
			CandidateDTO cDTO = new CandidateDTO();
			cDTO = conv.fromEntity(list.get(0));

			return cDTO;
		} else {
			return null;
		}

	}

	@Override
	public CandidateDTO checkIfExists(String firstname, String lastname,
			String email) {
		List<Candidate> candidateList = new ArrayList<Candidate>();

		candidateList = em
				.createQuery(
						"SELECT c FROM Candidate c WHERE c.firstname LIKE :firstname AND c.lastname LIKE :lastname AND c.email LIKE :email",
						Candidate.class).setParameter("firstname", firstname)
				.setParameter("lastname", lastname)
				.setParameter("email", email).getResultList();

		CandidateDTO candidateDTO = null;
		if (candidateList.size() > 0) {
			candidateDTO = conv.fromEntity(candidateList.get(0));
		}
		return candidateDTO;
	}
}
